package cn.agiledata.bank.transaction.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

import cn.agiledata.bank.audit.service.AuditService;
import cn.agiledata.bank.common.dao.TransactionTemplate;
import cn.agiledata.bank.common.service.SetUpUses;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.ForeDict;
import cn.agiledata.bank.common.util.UniqueIdentityUtil;
import cn.agiledata.bank.hibernateBean.EsPrePayTrans;
import cn.agiledata.bank.hibernateBean.Transaction;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class EsPerPayTransForBussiness extends EsPrePayTransFactoryAbstract {

	private AuditService getAuditService() {
		return (AuditService) IPBAppFactory.getObj("auditService");
	}
	
	private TransactionTemplate getTransactionTemplate(){
	    return (TransactionTemplate)IPBAppFactory.getObj(ForeDict.TRANSACTION_TEMPLATE);
	}

	public synchronized void saveTransaction(EsPrePayTrans esPrePayTrans) {
		// TODO Auto-generated method stub
		List bankList = new ArrayList();

		// 如果是账户选择担保支付
		if (esPrePayTrans.getTradeType() != null
				&& esPrePayTrans.getTradeType().equals(
						Constant.TRADE_TYPE_ASSURE)) {
			/*
			 * //如果不是卖方发货操作并且不是等待卖方付款，就封装交易明细 if (!tran.getTransStatus().equals(
			 * Constant.TRANS_STATUS_WAITFORBUYERAFFIRM) &&
			 * !tran.getTransStatus().equals(
			 * Constant.TRANS_STATUS_WAITFORBUYERPAY)) {
			 *//**
				 * todo 如果既不是付款也不是收款,仅交易状态改变,例如申请退款
				 */
			/*
			 * if(tran.getOperate()==null){
			 *//**
				 * todo 似乎不需要做任何事情
				 */
			/*
			 * }else // 如果是付款操作 if
			 * (tran.getOperate().equals(Constant.MODEL_TYPE_PAYMONEY)) {
			 * bankList = ftd.fillTransactionBussiness(tran);
			 * ahoi.updateAll(tran.getTransDetails().toArray());
			 * ahoi.update(tran.getDebit(),tran.getAmount(),tran.getDebitFee(),Constant.PAY,tran
			 * ,tran.getCredit().getId()); } else if (tran.getOperate().equals(
			 * Constant.MODEL_TYPE_GATHERING)) {// 如果是收款操作
			 * 
			 * bankList = ftd.fillTransactionBussinessSave(tran);
			 * 
			 * if (tran.getTradeType().equals(Constant.TRADE_TYPE_ASSURE) &&
			 * (tran.getTransStatus().equals(
			 * Constant.TRANS_STATUS_TRANSACTIONSUCCESS) ||
			 * tran.getTransStatus().equals(
			 * Constant.TRANS_STATUS_WAITFORBUYER))) {
			 * ahoi.updateAll(tran.getTransDetails().toArray());
			 * ahoi.update(tran.getCredit(),tran.getAmount(),tran.getCreditFee(),Constant.RECEIVE,tran
			 * ,tran.getDebit().getId()); } }
			 * getHibernateTemplate().saveOrUpdateAll(bankList); }
			 * getHibernateTemplate().saveOrUpdate(tran);
			 */
			getHibernateTemplate().saveOrUpdate(esPrePayTrans);

		} else {// 如果账户选择直接支付

			/*
			 * //如果是直接付款或者收款过程中的付款方确认操作 if ((tran.getOperate()!= null &&
			 * tran.getOperate().equals(Constant.MODEL_TYPE_PAYMONEY)) ||
			 * tran.getTransStatus().equals(Constant.TRANS_STATUS_TRANSACTIONSUCCESS)) {
			 * bankList = ftd.fillTransactionWithOutAssure(tran);
			 * getHibernateTemplate().saveOrUpdateAll(bankList);
			 * ahoi.update(tran.getCredit(),tran.getAmount(),tran.getCreditFee(),Constant.RECEIVE,tran
			 * ,tran.getDebit().getId());
			 * ahoi.update(tran.getDebit(),tran.getAmount(),tran.getDebitFee(),Constant.PAY,tran
			 * ,tran.getCredit().getId());
			 * ahoi.updateAll(tran.getTransDetails().toArray()); }
			 */
			getHibernateTemplate().saveOrUpdate(esPrePayTrans);
			// ahoi.updateTranHis(tran);
		}

	}
	public void updateTransaction(EsPrePayTrans esPrePayTrans) {
		esPrePayTrans.setUpdateTime(new Date());
		esPrePayTrans.setTransStatus(Constant.ES_TRANS_STATUS_FINISH);
		esPrePayTrans.setBankSeq(Constant.TOPCARD);
		getHibernateTemplate().saveOrUpdate(esPrePayTrans);
	}

	public void esPrePayTransToTransaction(EsPrePayTrans esPrePayTrans) {
		// List bankList = new ArrayList();
		// 拷贝EsPrePayTrans对象到trans
		Transaction trans = null;
        String transId = esPrePayTrans.getTransId();
        if (transId != null && transId.length() > 0) {
            trans = getTransactionTemplate().getTransactionByTranId(transId);
        }
        if (trans == null) {
            trans = new Transaction();
        }
		try {
			PropertyUtils.copyProperties(trans, esPrePayTrans);
		} catch (Exception e) {

			e.printStackTrace();
		}
		esPrePayTrans.setTransStatus(Constant.ES_TRANS_STATUS_FINISH);
		getHibernateTemplate().saveOrUpdate(esPrePayTrans);

		// 修正trans状态
		trans.setId(null);
		if(trans.getTradeType().equals(Constant.TRANTYPE_ASSURE))
			trans.setTransStatus(Constant.TRANS_STATUS_WAITFORBUYER);
		else
			trans.setTransStatus(Constant.TRANS_STATUS_TRANSACTIONSUCCESS);
        //记账模型：付款
        trans.setOperate(Constant.MODEL_TYPE_PAYMONEY);
		trans.setAuditTrans("0");
		if(String.valueOf(trans.getActionType().getId()).equals(Constant.ACTION_TYPE_C)) {	//账户间交易的回单生成
			trans.setVoucherCode(UniqueIdentityUtil.getTransferPaperSequence());
		}
		if(Constant.TRANTYPE_NOASSURE.equals(trans.getTradeType())) {//如果是无担保交易
			trans.setEndDate(new Date());	
		}
		trans.setUpdateTime(new Date());
		GetTransaction getTran = new GetTransactionImpl();
		//退款交易的新增处理逻辑:
	    //当交易类型为退款.但bank_seq为空的时候.说明该条交易之前为账户间交易. 直接走账户间交易流程.
	    //这里通过临时改变actiontype来实现
		String actionType = String.valueOf(esPrePayTrans
	        	.getActionType().getId());
		if (Constant.ACTION_TYPE_R.equals(actionType) && esPrePayTrans.getBankSeq() == null){
	        actionType = Constant.ACTION_TYPE_C;
	    }
	    //end
		TransactionFactory tt = getTran.getTransaction(actionType);

		tt.saveTransaction(trans);
		
		//发送邮件
		SetUpUses mail = new SetUpUses();
		try {
			mail.sendSetup(trans, trans.getDebit(), null, null, null, null,
					null);
			mail.sendSetup(trans, trans.getCredit(), null, null, null, null,
					null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		/*
		 * UniqueIdentityUtil uiu = new UniqueIdentityUtil();
		 * if(String.valueOf(trans.getActionType().getId()).equals(Constant.ACTION_TYPE_C)) {
		 * //账户间交易的回单生成 trans.setVoucherCode(uiu.getTransferPaperSequence()); }
		 * 
		 * //如果是账户选择担保支付 if (trans.getTradeType()!= null &&
		 * trans.getTradeType().equals(Constant.TRADE_TYPE_ASSURE)){
		 * //如果不是卖方发货操作并且不是等待卖方付款，就封装交易明细 if (!trans.getTransStatus().equals(
		 * Constant.TRANS_STATUS_WAITFORBUYERAFFIRM) &&
		 * !trans.getTransStatus().equals(
		 * Constant.TRANS_STATUS_WAITFORBUYERPAY)) {
		 *//**
			 * todo 如果既不是付款也不是收款,仅交易状态改变,例如申请退款
			 */
		/*
		 * if(trans.getOperate()==null){
		 *//**
			 * todo 似乎不需要做任何事情
			 */
		/*
		 * }else // 如果是付款操作 if
		 * (trans.getOperate().equals(Constant.MODEL_TYPE_PAYMONEY)) { bankList =
		 * ftd.fillTransactionBussiness(trans);
		 * ahoi.updateAll(trans.getTransDetails().toArray());
		 * ahoi.update(trans.getDebit(),trans.getAmount(),trans.getDebitFee(),Constant.PAY,trans
		 * ,trans.getCredit().getId()); } else if (trans.getOperate().equals(
		 * Constant.MODEL_TYPE_GATHERING)) {// 如果是收款操作
		 * 
		 * bankList = ftd.fillTransactionBussinessSave(trans);
		 * 
		 * if (trans.getTradeType().equals(Constant.TRADE_TYPE_ASSURE) &&
		 * (trans.getTransStatus().equals(
		 * Constant.TRANS_STATUS_TRANSACTIONSUCCESS) ||
		 * trans.getTransStatus().equals( Constant.TRANS_STATUS_WAITFORBUYER))) {
		 * ahoi.updateAll(trans.getTransDetails().toArray());
		 * ahoi.update(trans.getCredit(),trans.getAmount(),trans.getCreditFee(),Constant.RECEIVE,trans
		 * ,trans.getDebit().getId()); } }
		 * getHibernateTemplate().saveOrUpdateAll(bankList); }
		 * getHibernateTemplate().saveOrUpdate(trans);
		 * 
		 * }else{//如果账户选择直接支付
		 * 
		 * //如果是直接付款或者收款过程中的付款方确认操作 if ((trans.getOperate()!= null &&
		 * trans.getOperate().equals(Constant.MODEL_TYPE_PAYMONEY)) ||
		 * trans.getTransStatus().equals(Constant.TRANS_STATUS_TRANSACTIONSUCCESS)) {
		 * bankList = ftd.fillTransactionWithOutAssure(trans);
		 * getHibernateTemplate().saveOrUpdateAll(bankList);
		 * ahoi.update(trans.getCredit(),trans.getAmount(),trans.getCreditFee(),Constant.RECEIVE,trans
		 * ,trans.getDebit().getId());
		 * ahoi.update(trans.getDebit(),trans.getAmount(),trans.getDebitFee(),Constant.PAY,trans
		 * ,trans.getCredit().getId());
		 * ahoi.updateAll(trans.getTransDetails().toArray()); }
		 * getHibernateTemplate().saveOrUpdate(trans);
		 * ahoi.updateTranHis(trans); } // 发邮件 SetUpUses mail = new SetUpUses();
		 * try { mail.sendSetup(trans, trans.getDebit(), null, null, null, null,
		 * null); mail.sendSetup(trans, trans.getCredit(), null, null, null,
		 * null, null); } catch (Exception e) { // TODO Auto-generated catch
		 * block e.printStackTrace(); }
		 */
	}

}